// ajaxTools <?php
/**
 * Useful Ajax Tools, by Olivier St-Laurent
 *
 * ajaxSubmitForm() // Bypass normal submit event of a form
 *  Usage :
 *          <form action="..." method="post" onsubmit="return ajaxSubmitForm(this, {OPTIONS});">
 *              ...
 *          </form>
 *
 *
 * ajax() and getAjax() // Simple functions to fetch ajax content quickly. getAjax() will RETURN the response.
 *  Usage :
 *          ajax('URL', {DATA}, {OPTIONS});
 *
 *
 * loadAjax() // Similar to jQuery load()
 *  Usage :
 *          loadAjax('URL', {DATA}, 'CONTAINER', {OPTIONS}|CALLBACK )
 *
 *
 *///?>

function ajaxSubmitForm(formObj, options) {
    if (typeof options === 'function') {
        options = {success : options};
    }
    if (typeof options === 'string') {
        options = {url : options};
    }
    if (typeof options !== 'object') {
        options = {};
    }

    var formData = {};
    formObj = $(formObj)[0] || document.forms[formObj] || document.getElementById(formObj);
    var elems = formObj.elements;

    for (var i = 0;
        i < elems.length;
        i++) {
        if ((elems[i].type != 'radio' && elems[i].type != 'checkbox') ||
            ((elems[i].type == 'radio' || elems[i].type == 'checkbox') && elems[i].checked == true)
            ) {
            if (elems[i].name.match(/\[\]$/g)) {
                //If multiple inputs are named with empty bracket (name="elem[]"), they will be put in an array for themselves
                var nameNoBracket = elems[i].name.replace('[]', '');
                if (typeof formData[nameNoBracket] === "undefined" || !(formData[nameNoBracket] instanceof Array)) {
                    formData[nameNoBracket] = new Array();
                }
                if ($(elems[i]).attr('multiple')) { // Support for <select multiple>
                    for (var y in $(elems[i]).val()) {
                        formData[nameNoBracket].push($(elems[i]).val()[y]);
                    }
                } else {
                    formData[nameNoBracket].push(elems[i].value);
                }
            } else {
                formData[elems[i].name] = elems[i].value;
            }
        }
    }
    $.ajax($.extend({
        url : formObj.action || '',
        method : $(formObj).attr('method'),
        data : formData
    }, options));

    return false;
}

function ajax(url, data, options) {
    if (data === undefined) {
        data = {};
    }
    if (typeof options === 'function') {
        options = {success : options};
    }
    if (options === undefined) {
        options = {};
    }
    return $.ajax($.extend({
        url : url,
        data : data,
        method : 'POST'
    }, options));
}

function getAjax(url, data, options) {
    var self = this;
    this.returnValue = null;
    if (typeof options === 'function') {
        options = {success : options};
    }
    if (options === undefined) {
        options = {};
    }
    options.success = function(response) {
        self.returnValue = response;
    };
    options.async = false;
    ajax(url, data, options);
    return this.returnValue;
}

function loadAjax(url, data, container, options) {
    var self = this;
    if (typeof data !== 'object') data = {};
    this.successCallback = null;
    if (typeof options === 'object' && typeof options.success === 'function') {
        this.successCallback = options.success;
    }
    if (typeof options === 'function') {
        this.successCallback = options;
        options = {};
    }
    if (typeof options !== 'object') options = {};
    options.success = function(response) {
        if (typeof container !== undefined) {
            $(container).html(response);
        }
        if (typeof self.successCallback === 'function') {
            self.successCallback(response);
        }
    };
    $.ajax($.extend({
        url : url,
        method : 'POST',
        data : data
    }, options));
}
